/*
 * 英才汇硕信息科技有限公司 拥有本软件版权 2021  并保留所有权利。
 * Copyright 2021, YCHS Information&Science Technology Co.,Ltd,
 * All rights reserved.
 */
package com.ssy.dao;

import cn.hutool.crypto.SecureUtil;
import com.ssy.domain.user;
import com.ssy.util.DbUtilHikari;
import sun.security.x509.URIName;

import java.security.Security;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户表操作
 * @author ssy
 * @version 1.0
 */
public class userdao {
    public List<user> selectalluser(){
        List<user> userList=new ArrayList<user>();
        Connection conn =null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;

        conn=DbUtilHikari.getConnection();
        String sql="SELECT userid,username,userpsw FROM users";
        try {
            pstmt =conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while (rs.next()){
                user user=new user();
                user.setUserid(rs.getInt("userid"));
                user.setUsername(rs.getString("username"));
                user.setUserpsw(rs.getString("userpsw"));
                userList.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(rs,pstmt,conn);
        }

        return userList;
    }

    public int updateuser(int userid,String uname,String upsw){
        int flag=0;
        Connection conn=null;
        PreparedStatement pstmt=null;
        conn =DbUtilHikari.getConnection();
        String sql="UPDATE users SET username=?,userpsw=? WHERE userid=?";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,uname);
            pstmt.setString(2,SecureUtil.md5(upsw));
            pstmt.setInt(3,userid);
            flag=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(pstmt,conn);
        }
        return flag;
    }

    public int deleteuser(int userid){
        int flag=0;
        Connection conn =null;
        PreparedStatement pstmt=null;
        conn=DbUtilHikari.getConnection();
        String sql="DELETE FROM users WHERE userid=?";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,userid);
            flag=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(pstmt,conn);
        }

        return flag;
    }


    public boolean login(String uname,String upsw){
        boolean flag=false;
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        conn=DbUtilHikari.getConnection();
        String sql = "SELECT * FROM users WHERE username=? AND userpsw=?";
        try {
            pstmt =conn.prepareStatement(sql);
            pstmt.setString(1,uname);
            pstmt.setString(2,SecureUtil.md5(upsw));
            rs=pstmt.executeQuery();
            if (rs.next()){
                flag=true;
            }else{
                flag=false;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(rs,pstmt,conn);
        }

        return flag;
    }


    public int inseruser(String uname,String upsw){
        //代表数据库连接
        Connection conn =null;
        //预编译对象
        PreparedStatement psrmt=null;
        int flag=0;
        conn = DbUtilHikari.getConnection();
        String sql = "INSERT INTO users(username,userpsw) VALUES(?,?)";
        try {
            psrmt=conn.prepareStatement(sql);
            psrmt.setString(1,uname);
            psrmt.setString(2, SecureUtil.md5(upsw));
            flag=psrmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(psrmt,conn);
        }

        return flag;
    }
}
